﻿package birdry.ui
{
	import birdry.manager.TweenManager;
	import com.greensock.TweenLite;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	* ...
	* @author makeFont
	*/
	public class ScrollBar_Controller extends EventDispatcher	{
		
		public static const BAR_OVER:String = "bar_over";
		public static const BAR_OUT:String = "bar_out";
		public static const BAR_DOWN:String = "bar_down";
		public static const BAR_UP:String = "bar_up";
		
		var _bar:Sprite;
		var _barBg:Sprite;
		
		var _min:Number;
		var _max:Number;
		var _xy:String;
		var _wh:String;
		
		var _ratio:Number=0;
		
		public function get ratio(): Number {
			return _ratio;
		}
		public function set ratio(value:Number):	void	{
			if (value < 0)	value = 0;
			if (value > 1)	value = 1;
			moveBar(value);
			_ratio = value;
		}
		
		public function ScrollBar_Controller(bar:Sprite, barBg:Sprite, xy:String="y")	{
			_bar = bar;
			_barBg = barBg;
			_xy = xy;
			_wh = (xy == "y") ? "height" : "width";
			setLayout();
			addEvent();
		} 
		
		private function setLayout(): void	{
			_min = _barBg[_xy];
			_max = _barBg[_xy] + _barBg[_wh] - _bar[_wh];
			_bar[_xy] = _barBg[_xy];
		}
		
		private function addEvent(): void	{
			_bar.addEventListener(MouseEvent.MOUSE_OVER, onBarOver);
			_bar.addEventListener(MouseEvent.MOUSE_OUT, onBarOut);
			_bar.addEventListener(MouseEvent.MOUSE_DOWN, onBarDown);
		}
		
		private function onBarOver( e: MouseEvent ): void		{
			dispatchEvent(new ScrollBarEvent( ScrollBarEvent.BAR_OVER ) );
		}
		
		private function onBarOut( e: MouseEvent ): void	{
			dispatchEvent(new ScrollBarEvent( ScrollBarEvent.BAR_OUT ) );
		}
		
		private function onBarDown(e:MouseEvent):void	{
			_bar.stage.addEventListener(MouseEvent.MOUSE_UP, dragStop);
			_bar.addEventListener(Event.ENTER_FRAME, onEnter);
			if (_xy == "y")	{
				_bar.startDrag(false, new Rectangle(_bar.x, _barBg.y, 0, _barBg.height - _bar.height));
			}	else	{
				_bar.startDrag(false, new Rectangle(_barBg.x, _bar.y, _barBg.width - _bar.width, 0));
			}
			dispatchEvent(new Event( ScrollBar_Controller.BAR_DOWN ) );
		}
		
		private function dragStop(e:MouseEvent):void	{
			_bar.stage.removeEventListener(MouseEvent.MOUSE_UP, dragStop);
			_bar.stopDrag();
			_bar.removeEventListener(Event.ENTER_FRAME, onEnter);
			dispatchEvent(new Event( ScrollBar_Controller.BAR_OUT ) );
			dispatchEvent(new Event( ScrollBar_Controller.BAR_UP ) );
		}
		
		private function onEnter(e:Event):void	{
			ratio = (_bar[_xy] - _barBg[_xy]) / (_max - _min);
			dispatchEvent(new Event( ScrollBar_Controller.BAR_OVER ) );
		}
		
		private function moveBar(value:Number):void 		{
			_bar[_xy] = _barBg[_xy] + (_max - _min) * value;
		}
		
		public function init(tartgetRatio:Number, completeFunction:Function=null, speed:Number=0.3): void	{
			TweenLite.to( this, speed, { 	ratio:tartgetRatio, ease:TweenManager.Expo_easeOut, 
													onUpdate:function()	{
														dispatchEvent(new Event( ScrollBar_Controller.BAR_OVER ) );
													},
													onComplete:function()	{
														if (completeFunction != null)	completeFunction();
													}
												});
		}
		
		///-----------------			버튼 제어에 해당되는 기능		--------------------///
		private var _btDirect:String;
		private var _btAccSpeed:Number  = 0;
		public function btDown( direct:String )	{
			_btDirect = direct;
			_bar.stage.addEventListener(Event.ENTER_FRAME, onBtDown);
		}
		
		private function onBtDown( e: Event ): void	{
			_btAccSpeed += 0.01 * (1 - _btAccSpeed);
			if (_btDirect == "-")	ratio -= _btAccSpeed;
			if (_btDirect == "+")	ratio += _btAccSpeed;
			dispatchEvent(new Event( ScrollBar_Controller.BAR_OVER ) );
		}
		
		public function btUp(): void	{
			_btAccSpeed = 0;
			_bar.stage.removeEventListener(Event.ENTER_FRAME, onBtDown);
		}
		///--------------------------------------------------------------------------///
	}	// class
	
}	// package